home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 299_01 / crews5.ex < prev    next >
Text File  |  1989-12-28  |  7KB  |  214 lines

  1.  
  2.  
  3. Example 5.  Public Interface Between MEL and Any Application Program
  4. Using It.  (Excerpted from mel.h header file.)
  5.  
  6. /* if using MEL for input (#define MEL_INPUT), then must 
  7.    define the MEL input data object: */ 
  8. #ifdef MEL_INPUT 
  9.         
  10. /* firstly, define input constants (all must be 
  11.    CUSTOMIZED for specific application program): */ 
  12.         
  13. #define MELI_MAX_DESCRIP_STR_LEN 256 
  14.     /* maximum number of characters in any input descriptor 
  15.        string. */ 
  16. #define MELI_MAX_PARAMS 6 
  17.     /* maximum number of parameters for any descriptor (min 
  18.        num = 1). */ 
  19. #define MELI_MAX_PARAM_STR_LEN 80 
  20. #define MELI_MAX_PARAM_ARRAY_STR_LEN 1 
  21.     /* largest allowable parameter string lengths (min size 
  22.        = 1) */ 
  23. #define MELI_MAX_PARAM_INT_ARRAY_LEN 1 
  24. #define MELI_MAX_PARAM_REAL_ARRAY_LEN 1 
  25. #define MELI_MAX_PARAM_STR_ARRAY_LEN 1 
  26.     /* maximum number of elements in parameter data arrays 
  27.        (min = 1). */ 
  28. #define MELI_UNITS_STR_LEN 80 
  29.     /* maximum length of units associated with any param 
  30.        (min = 1) */ 
  31.         
  32. /* secondly, define input data structures: */ 
  33.         
  34. union meli_param_data { 
  35.     int integer;  /* also holds boolean type */
  36.     double real; 
  37.     char string[MELI_MAX_PARAM_STR_LEN+1]; 
  38.     int integer_array[MELI_MAX_PARAM_INT_ARRAY_LEN]; 
  39.     double real_array[MELI_MAX_PARAM_REAL_ARRAY_LEN]; 
  40.     char string_array[MELI_MAX_PARAM_STR_ARRAY_LEN] 
  41.                 [MELI_MAX_PARAM_ARRAY_STR_LEN+1]; 
  42. }; 
  43. /* this is used for input parameter data. it may either be 
  44.    an integer, real, string, array of integers, array of 
  45.    reals, or an array of strings. (to save space a union was 
  46.    used.) */ 
  47.         
  48. /* thirdly, define input variables: */ 
  49.         
  50. char meli_descriptor_string[MELI_MAX_DESCRIP_STR_LEN+1]; 
  51.     /* global storage for the input descriptor string. */ 
  52.         
  53. /* lastly, define input functions (typically they return 0 
  54.    if no error encountered, else some nonzero error 
  55.    code): */ 
  56.         
  57. int meli_file(FILE *meli_file_handle); 
  58.     /* read a descriptor string from the input stream and 
  59.        call meli(). also, put copy of string read into 
  60.        meli_descriptor_string. */ 
  61. int meli(void); 
  62.     /* translate meli_descriptor_string and put information 
  63.        into a private data structure (meli_datum). */ 
  64. char *meli_descrip_type(void); 
  65.     /* return pointer to name of type of descriptor read by 
  66.        meli(). */ 
  67. int meli_num_params(void); 
  68.     /* return number of parameters read by meli(). */ 
  69. int meli_param(int param_num, char *param, union 
  70.     meli_param_data *data, char *units, int *array_len, int 
  71.     *unknown_flag); 
  72.     /* fill arguement list with param_num'th parameter read 
  73.        by meli(). (start with param_num = 0.) */ 
  74. int meli_data(char *param, union meli_param_data *data, 
  75.     char *units, int *array_len, int *unknown_flag); 
  76.     /* see if *param was input. if it was, then fill 
  77.        argument list with data from meli_datum. */ 
  78.  
  79. #endif /* MEL_INPUT */ 
  80.         
  81. /* if using MEL for output, must define the MEL output data 
  82.    object: */ 
  83. #ifdef MEL_OUTPUT 
  84.         
  85. /* firstly, define output constants (all must be 
  86.    CUSTOMIZED): */ 
  87.         
  88. #define MELO_MAX_DESCRIP_STR_LEN 256 
  89.     /* how many characters can be in an output descriptor 
  90.        string? */ 
  91. #define MELO_MAX_PARAMS 6 
  92.     /* maximum number of parameters for any descriptor. */ 
  93. #define MELO_MAX_PARAM_STR_LEN 80 
  94. #define MELO_MAX_PARAM_ARRAY_STR_LEN 1 
  95.     /* largest allowable parameter string length. */ 
  96. #define MELO_MAX_PARAM_INT_ARRAY_LEN 1 
  97. #define MELO_MAX_PARAM_REAL_ARRAY_LEN 1 
  98. #define MELO_MAX_PARAM_STR_ARRAY_LEN 1 
  99.     /* maximum number of elements in array of parameter 
  100.        data. */ 
  101. #define MELO_UNITS_STR_LEN 80 
  102.     /* maximum string length of any units associated with a 
  103.        param. */ 
  104.         
  105. /* secondly, define output data structures: */ 
  106.         
  107. union melo_param_data { 
  108.     int integer; 
  109.     double real; 
  110.     char string[MELO_MAX_PARAM_STR_LEN+1]; 
  111.     int integer_array[MELO_MAX_PARAM_INT_ARRAY_LEN]; 
  112.     double real_array[MELO_MAX_PARAM_REAL_ARRAY_LEN]; 
  113.     char string_array[MELO_MAX_PARAM_STR_ARRAY_LEN] 
  114.                 [MELO_MAX_PARAM_ARRAY_STR_LEN+1]; 
  115. }; 
  116. /* this is for output parameter data. it may either be an 
  117.    integer, real, string, array of integers, array of reals, or 
  118.    an array of strings. (to save space a union was used.) */ 
  119.         
  120. /* thirdly, define output variables: */ 
  121.         
  122. char melo_descriptor_string[MELO_MAX_DESCRIP_STR_LEN+1]; 
  123.     /* global storage for the output descriptor string. */ 
  124.         
  125. /* lastly, define output functions (typically return 0 if no 
  126.    error): */ 
  127.         
  128. int melo_init(char *descrip_type); 
  129.     /* initialize private data structure (melo_datum) to 
  130.        accept parameter data from following functions. 
  131.        output descriptor type will be descrip_type. returns 
  132.        0 if no errors were encountered. */ 
  133. int melo_data(char *param, union melo_param_data *data, char 
  134.     *units, int array_len, int unknown_flag); 
  135.     /* put data for parameter *param into the proper place 
  136.        in melo_datum. returns zero if no errors were 
  137.        encountered. */ 
  138. void melo(int melo_verbose_flag); 
  139.     /* takes the information in melo_datum and translates it 
  140.        into melo_descriptor_string. user must set 
  141.        melo_verbose_flag = 1 to make output as readable as 
  142.        possible, set it equal to zero to make output as 
  143.        terse as possible (and still remain in MEL 
  144.        format). */ 
  145. int melo_file(FILE *melo_file_handle, int melo_verbose_flag); 
  146.     /* take the information in melo_datum, translate it into 
  147.        melo_descriptor_string, and output it to file. */ 
  148.  
  149. #endif /* MEL_OUTPUT */
  150.  
  151. /* now define data objects common to both input and 
  152.    output: */ 
  153.  
  154. /* if an error occurs, MEL will try and tell you what 
  155.    happened. so define required error handling 
  156.    information: */ 
  157.  
  158. #define MEL_MAX_ERR_MSG_LEN 80 
  159.  
  160. struct mel_errors { 
  161.     enum {   /* which error occured? */ 
  162.         mel_no_err,
  163.         mel_read_err, 
  164.         mel_write_err, 
  165.         mel_end_of_file_err, 
  166.         mel_end_of_data_err, 
  167.         mel_syntax_err, 
  168.         mel_unknown_descrip_name_err, 
  169.         mel_unknown_param_name_err, 
  170.         mel_missing_param_name_err,         
  171.         mel_param_data_err, 
  172.         mel_missing_paren_err, 
  173.         mel_too_many_param_err,         
  174.         mel_missing_bracket_err, 
  175.     } type; 
  176.     int start_line;   /* on which lines did err occur? */ 
  177.     int end_line;     /* (meaningful for input only.) */ 
  178.     char msg[MEL_MAX_ERR_MSG_LEN+1];   
  179.             /* additional info describing err */ 
  180. } mel_err;  /* (not same as messages below). */ 
  181.  
  182. #define MEL_MAX_NUM_ERR_MESSAGES 13 
  183.  
  184. #ifdef MEL_INIT 
  185.  
  186. /* the following describes each type of enumerated error: */ 
  187. char mel_err_msg[MEL_MAX_NUM_ERR_MESSAGES]
  188.             [MEL_MAX_ERR_MSG_LEN+1] 
  189.     ={"No errors encountered", 
  190.       "Can't read file", 
  191.       "Can't write file", 
  192.       "Unexpected end of file encountered", 
  193.       "End of input data encountered", 
  194.       "Descriptor/parameter syntax error", 
  195.       "Unknown descriptor name", 
  196.       "Unknown parameter name", 
  197.       "A (or another) parameter name was expected but is "
  198.           "missing", 
  199.       "Unable to read parameter value(s) for this "
  200.           "descriptor", 
  201.       "Missing right parenthesis while reading units", 
  202.       "Too many (or duplicate) parameters given for this "
  203.           "descriptor", 
  204.       "Missing brackets around array data"}; 
  205.  
  206. #else 
  207.  
  208. extern char mel_err_msg[MEL_MAX_NUM_ERR_MESSAGES]
  209.                 [MEL_MAX_ERR_MSG_LEN+1]; 
  210.  
  211. #endif /* MEL_INIT */
  212.  
  213.  
  214.